﻿// MIT License
// 开源地址：https://gitee.com/co1024/AbcMvc
// Copyright (c) 2021-2023 1024
// Abc.Mvc=Furion+EF+SqlSugar+Pear layui admin.

using Furion.EventBus;

namespace Abc.Core.EventBus;
/// <summary>
/// 事件执行监视器
/// </summary>
public class ToDoEventHandlerMonitor : IEventHandlerMonitor
{
    private readonly Serilog.ILogger _logger;

    public ToDoEventHandlerMonitor()
    {
        _logger = AppEx.GetSeriLogger("EventBus\\Monitor");
    }

    public Task OnExecutingAsync(EventHandlerExecutingContext context)
    {
        _logger.Information($"执行之前 -> EventId:{context.Source.EventId} \r\nPayload:{context.Source.Payload.ToJson()}");
        return Task.CompletedTask;
    }

    public Task OnExecutedAsync(EventHandlerExecutedContext context)
    {
        _logger.Information($"执行之后 -> EventId:{context.Source.EventId} \r\nPayload:{context.Source.Payload.ToJson()}");

        if (context.Exception != null)
        {
            _logger.Error($"执行异常 -> EventId:{context.Source.EventId} \r\nPayload:{context.Source.Payload.ToJson()}\r\n异常：{context.Exception.ToStringEx()}");
        }

        return Task.CompletedTask;
    }
}
